package conf

import (
	"bytes"
	"encoding/json"
	"io/ioutil"
	"log"
	"os"
	"time"
)

var (
	// log conf
	LogFlag = log.Lshortfile

	// gate conf
	PendingWriteNum        = 2000
	MaxMsgLen       uint32 = 4096
	HTTPTimeout            = 10 * time.Second
	LenMsgLen              = 2
	LittleEndian           = false

	// skeleton conf
	GoLen              = 10000
	TimerDispatcherLen = 10000
	AsynCallLen        = 10000
	ChanRPCLen         = 10000

	// 项目配置项
	CustomConfig *ProjectConfig
)

// ================================================================
// 项目配置
type ProjectConfig struct {
	MongoDB MongoDB
	Game    Game
}

type MongoDB struct {
	UseAuth       bool
	URL           string
	DBName        string
	Account       string
	Password      string
	AuthMeshanism string
}

type Game struct {
	MatchingLevelRange int //匹配等级范围
}

// 写出项目配置样本
func WriteProjectConfigExample() []byte {
	config := ProjectConfig{MongoDB: MongoDB{
		UseAuth:       true,
		URL:           "mongodb://localhost:27011,localhost:27012",
		DBName:        "",
		Account:       "",
		Password:      "",
		AuthMeshanism: "SCRAM-SHA-1",
	}}

	// 转换成JSON后再格式化返回文本
	byte, _ := json.Marshal(&config)
	var buf bytes.Buffer
	json.Indent(&buf, byte, "", "  ")
	return buf.Bytes()
}

// 读取项目配置
func ReadProjectConfig(path string) (*ProjectConfig, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}

	content, err := ioutil.ReadAll(file)
	if err != nil {
		return nil, err
	}
	var projectConfig ProjectConfig
	err = json.Unmarshal(content, &projectConfig)
	if err != nil {
		return nil, err
	}
	return &projectConfig, nil
}
